import React, {PureComponent} from "react";
import { CloudDownloadOutlined } from '@ant-design/icons';
import { Button, Checkbox } from "antd";
import cs from "classnames"
import "./item.less"
import PropTypes from "prop-types";

const fileDesc = {
	PLT: '纸样文件',
	BOKE: '博克CAD'
}

function getFileName(originName) {
	if (!originName) return ""
	const i = originName.indexOf('/')
	if (i > -1) {
		return originName.substring(i + 1)
	}
	return originName
}

export default class FileItem extends PureComponent {
	render() {
		const {
			readOnly, file, onFileCheck, isCheck, toDownload, type, data, price
		} = this.props
		return (
            <div className={'down-file-item plt'}>
				<div>

					{
						readOnly
							? undefined
							: <Checkbox value={file.id} onChange={onFileCheck(file)} checked={isCheck(file)}/>
					}

					<div className={cs('file-type', `${type.toLocaleLowerCase()}`)}></div>

					<div className={'down-file-item-name'}>
						{getFileName(file['originalName'])}
					</div>

					<span className={cs('item-price', {bought: file['accessible'] && !readOnly})}>
            [ {file['accessible'] && !readOnly ? '已购买' : `￥${(price / 100 || 0).toFixed(2)}`} ]
          </span>
				</div>

				{readOnly ? undefined :
					<Button type="link" icon={<CloudDownloadOutlined />} onClick={toDownload(file)}></Button>}
			</div>
        );

	}
}

FileItem.propTypes = {
	readOnly: PropTypes.bool,
	data: PropTypes.object,
	file: PropTypes.object,
	onFileCheck: PropTypes.func,
	isCheck: PropTypes.func,
	toDownload: PropTypes.func,
	type: PropTypes.string
}